package com.cqs.leetcode.dp;


/**
 * lixw
 * 2021/11/12
 **/
public class GuessNumberII375 {


    public int getMoneyAmount(int n) {
        int[][] dp = new int[n + 1][n + 1];
        for (int delta = 1; delta < n; delta++) {
            for (int i = 1; i < n && i + delta <= n; i++) {
                int j = i + delta;
                dp[i][j] = Integer.MAX_VALUE;
                for (int k = i; k <= i + delta; k++) {
                    int val = k + dp[i][k - 1];
                    if (k + 1 <= n) {
                        val = k + Math.max(dp[i][k - 1], dp[k + 1][j]);
                    }
                    dp[i][j] = Math.min(dp[i][j], val);
                }
            }
        }
        return dp[1][n];
    }

    public static void main(String[] args) {
        GuessNumberII375 g = new GuessNumberII375();

        for (int i = 2; i < 100; i++) {
            System.out.println(i + "\t--> " + g.getMoneyAmount(i));
        }
    }

    public int getMoneyAmount2(int n) {
        int[][] dp = new int[n + 1][n + 1];
        for (int i = 1; i <= n; i++) {
            for (int j = i; j <= n; j++) {
                dp[i][j] = Integer.MAX_VALUE;
                for (int k = i; k <= j; k++) {
                    if (k - 1 - i <= 0) {
                        dp[i][k - 1] = 0;
                    }
                    int val = k + dp[i][k - 1];
                    if (k + 1 < n) {
                        if (j - k - 1 <= 0) {
                            dp[k + 1][j] = 0;
                        }
                        val = k + Math.max(dp[i][k - 1], dp[k + 1][j]);
                    }
                    dp[i][j] = Math.min(val, dp[i][j]);
                }
            }
        }
        return dp[1][n];
    }
}
